<?php
/**
 * Created by JetBrains PhpStorm.
 * User: q
 * Date: 14-8-17
 * Time: 上午9:24
 * To change this template use File | Settings | File Templates.
 */
class weixin extends CI_Controller{
    private $token = 'ido';   //无限分类视图路径

    public function __construct(){
        parent::__construct();
    }
    public function valid()
    {
        $this->responseMsg();

    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        $MsgType = $postObj->MsgType;
        if($MsgType == 'image'){
            $this->get_image($postObj);
        }elseif($MsgType == 'text'){
            $this->get_weight($postObj);
        }else{
            $text = '请发送体重（单位KG），将保存为今日体重;发送图片，将保存为今日身材图。';
            $this->re_test($postObj,$text);
        }
        //extract post data
    }

    public function get_weight($postObj){
        $this->load->model('weight_model');
        $fromUsername = $postObj->FromUserName;
        $fromUsername = (string)$fromUsername;
        $keyword = trim($postObj->Content);

        if(is_numeric($keyword)){
            $date =strtotime(date("Y-m-d"));
            $check = $this->weight_model->check($date,$fromUsername);
            if($check){
                $data = array(
                    'openid' => $fromUsername,
                    'weight' => $keyword,
                    'date' => $date,
                );
                $this->weight_model->insert($data);
                $text = date("Y-m-d").' 体重上传成功';
            }else{
                $this->weight_model->update($fromUsername,$keyword,$date);
                $text = date("Y-m-d").' 体重修改成功';
            }
        }else{
            $text = '请发送体重（单位KG），将保存为今日体重;发送图片，将保存为今日身材图。';
        }
        $text = $text.' <a href="http://keepdoing.sinaapp.com/index.php/curve?openid='.$fromUsername.'">点击查看曲线图</a>';

        $this->re_test($postObj,$text);
    }

    public function get_image($postObj){
        $this->load->model('weightpic_model');
        $fromUsername = $postObj->FromUserName;
        $fromUsername = (string)$fromUsername;
        $url = trim($postObj->PicUrl);

        $date =strtotime(date("Y-m-d"));
        $check = $this->weightpic_model->check($date,$fromUsername);
        if($check){
            $data = array(
                'openid' => $fromUsername,
                'pic' => $url,
                'date' => $date,
            );
            $this->weightpic_model->insert($data);
            $text = date("Y-m-d").' 身材照片上传成功';
        }else{
            $this->weightpic_model->update($fromUsername,$url,$date);
            $text = date("Y-m-d").' 身材照片修改成功';
        }
        $text = $text.' <a href="http://keepdoing.sinaapp.com/index.php/photo?openid='.$fromUsername.'">点击查看照片墙</a>';
        $this->re_test($postObj,$text);
    }

    public function final_time(){

    }

    public function re_test($postObj,$contentStr){

        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";

        $msgType = "text";
        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
        echo $resultStr;

    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = $this->token;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}